final_random <- final %>% filter(days <=150)

final_random$days <- final_random$days + 53

days_list <- 1:200
random <- vector("list", length(days_list))

days_vec <- c()
beta_vec <- c()
se_vec <- c()

for (i in seq_along(days_list)) {
  final_random$treatment_random <- final_random$days > days_list[i]
  random[[i]] <- lm(violence ~ days * treatment_random, data = final_random, weights = weight)
  days_vec[i] <- paste0("days_",i)
  beta_vec[i] <- summary(random[[i]])$coefficients[4]
  se_vec[i] <- summary(random[[i]])$coefficients[4,2]
}


violence_random <- data.frame(cbind(days_vec, beta_vec, se_vec))

violence_random <- violence_random %>% separate(days_vec, c("dd", "days")) %>% mutate(days = as.numeric(days) - 53,
                                                                                      se_vec = as.numeric(se_vec),
                                                                                      beta_vec = as.numeric(beta_vec))


##

days_vec <- c()
beta_vec <- c()
se_vec <- c()

for (i in seq_along(days_list)) {
  final_random$treatment_random <- final_random$days > days_list[i]
  random[[i]] <- lm(norms ~ days * treatment_random, data = final_random, weights = weight)
  days_vec[i] <- paste0("days_",i)
  beta_vec[i] <- summary(random[[i]])$coefficients[4]
  se_vec[i] <- summary(random[[i]])$coefficients[4,2]
}


norms_random <- data.frame(cbind(days_vec, beta_vec, se_vec))

norms_random <- norms_random %>% separate(days_vec, c("dd", "days")) %>% mutate(days = as.numeric(days) - 53,
                                                                                se_vec = as.numeric(se_vec),
                                                                                beta_vec = as.numeric(beta_vec))

##

days_vec <- c()
beta_vec <- c()
se_vec <- c()

for (i in seq_along(days_list)) {
  final_random$treatment_random <- final_random$days > days_list[i]
  random[[i]] <- lm(affpol ~ days * treatment_random, data = final_random, weights = weight)
  days_vec[i] <- paste0("days_",i)
  beta_vec[i] <- summary(random[[i]])$coefficients[4]
  se_vec[i] <- summary(random[[i]])$coefficients[4,2]
}


affpol_random <- data.frame(cbind(days_vec, beta_vec, se_vec))

affpol_random <- affpol_random %>% separate(days_vec, c("dd", "days")) %>% mutate(days = as.numeric(days) - 53,
                                                                                  se_vec = as.numeric(se_vec),
                                                                                  beta_vec = as.numeric(beta_vec))


random_interaction1 <- ggplot(violence_random, aes(x=beta_vec, y = days))+
  geom_pointrange(aes(xmin = (beta_vec - 1.96*se_vec), xmax = (beta_vec + 1.96*se_vec)), color = '#cc454e', size =.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  geom_vline(xintercept = 0, color = 'black', linetype=1)+
  coord_flip()+
  theme_bw()+
  labs(x="Random Election Day Interaction (Violence)",
       y = "Days Before/After Election")

random_interaction2 <- ggplot(norms_random, aes(x=beta_vec, y = days))+
  geom_pointrange(aes(xmin = (beta_vec - 1.96*se_vec), xmax = (beta_vec + 1.96*se_vec)), color = '#3d37bd', size =.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  geom_vline(xintercept = 0, color = 'black', linetype=1)+
  coord_flip()+
  theme_bw()+
  labs(x="Random Election Day Interaction (Norms)",
       y = "Days Before/After Election")

random_interaction3 <- ggplot(affpol_random, aes(x=beta_vec, y = days))+
  geom_pointrange(aes(xmin = (beta_vec - 1.96*se_vec), xmax = (beta_vec + 1.96*se_vec)), color = '#1c9906', size =.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  geom_vline(xintercept = 0, color = 'black', linetype=1)+
  coord_flip()+
  theme_bw()+
  labs(x="Random Election Day Interaction (AP)",
       y = "Days Before/After Election")

random_interaction <- suppressMessages(ggpubr::ggarrange(random_interaction1,random_interaction2,random_interaction3, ncol = 1))
suppressMessages(print(random_interaction))